package com.ruoyi.house.service.webmagic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component("scheduledTask")
public class ScheduledTask {

    Logger logger = LoggerFactory.getLogger(ScheduledTask.class);

    @Autowired
    private BuildingPipeline buildingPipeline;


    public void cronBuilding(){
        System.out.println("cron 打印时间："+ new Date());
        BuildingProcessor processor = new BuildingProcessor();
        Spider.create(processor).addPipeline(buildingPipeline)
                .addUrl(processor.getUrl())
//                .addUrl(processor.getBaseUrl())
//                .addUrl(processor.getTestBaseUrl())
//                .addUrl(processor.getYearSaleUrl())
                // 开启1个线程抓取
                .thread(20)
                // 启动爬虫
                .run();
        processor.close();
        System.out.println("cron 打印end时间："+ new Date());
    }

    /**
     * 获取全部楼盘小区
     */
    public void districtProcess(Integer districtStart){

        int driverNum = 10;


        int districtNum = 104;
        String url = "https://124.128.246.22:8090/onsaling/index.shtml";
        ArrayList<String> pageUrls = new ArrayList<String>();


        do {
            pageUrls.clear();
            if (districtStart == 1){
                url = "https://124.128.246.22:8090/onsaling/index.shtml";
            }else {
                url = "https://124.128.246.22:8090/onsaling/index_"+districtStart+".shtml?zn=all&pu=all&pn=&en=";
            }
            pageUrls.add(url);
            logger.error("start process url:"+url);
            startProcess(pageUrls,driverNum);
            logger.error("end process url:"+url);
//            url = "https://124.128.246.22:8090/onsaling/index_"+districtStart+".shtml?zn=all&pu=all&pn=&en=";
            districtStart++;
        }while (districtNum > districtStart);

    }


    /**
     * 处理过程
     * @param pageUrls
     * @param threadNum
     */
    public void startProcess(List<String> pageUrls, int threadNum) {

        System.out.println("cron 打印时间："+ new Date());

        String[] pages = new String[pageUrls.size()];
        BuildingProcessor processor = new BuildingProcessor(threadNum);
        Spider.create(processor).addPipeline(buildingPipeline)
                .addUrl(pageUrls.toArray(pages))
                // 开启线程抓取
                .thread(threadNum*threadNum)
                // 启动爬虫
                .run();

        processor.close();
        System.out.println("cron 打印end时间："+ new Date());
    }

    /**
     * 首页处理，各区年度销售每日累计、商品房类别每日签约数据
     * todo 各区月度销售每日累计
     */
    public void indexPageProcess(){

        int driverNum = 10;
        ArrayList<String> pageUrls = new ArrayList<String>();
        pageUrls.add(getBaseUrl());
        pageUrls.add(getYearSaleUrl());
        indexPageProcess(pageUrls,driverNum);

    }

    /**
     * 处理过程
     * @param pageUrls
     * @param threadNum
     */
    public void indexPageProcess(List<String> pageUrls, int threadNum) {

        System.out.println("cron 打印时间："+ new Date());

        String[] pages = new String[pageUrls.size()];
        BuildingProcessor processor = new BuildingProcessor();
        Spider.create(processor).addPipeline(buildingPipeline)
                .addUrl(pageUrls.toArray(pages))
                // 开启线程抓取
                .thread(threadNum)
                // 启动爬虫
                .run();

        processor.close();
        System.out.println("cron 打印end时间："+ new Date());
    }


    public String getUrl() {

        String url = "https://124.128.246.22:8090/onsaling/index.shtml";
        return url;
    }

    /**
     * 首页
     * @return
     */
    public String getBaseUrl(){
        String url = "https://124.128.246.22:8090/";
        return url;
    }

    /**
     * 年度售卖数据
     * @return
     */
    public String getYearSaleUrl(){
        String url = "https://124.128.246.22:8090/onsaling/yearsale.shtml";
        return url;
    }
}
